//
//  InviteDetailsViewController.m
//  SelfDriving
//
//  Created by apple on 16/7/2.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "InviteDetailsViewController.h"
#import <MessageUI/MFMessageComposeViewController.h>

@interface InviteDetailsViewController ()<MFMessageComposeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UIView *bgview;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *phone;

@property (nonatomic, strong) MFMessageComposeViewController * MScontroller;
@end

@implementation InviteDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _bgview.layer.cornerRadius = 3;
    _bgview.layer.borderColor = colorBG.CGColor;
    _bgview.layer.borderWidth = 0.5;
    _btn.layer.cornerRadius = 4;
    _name.text = [_dic objectForKey:@"name"];
    _phone.text = [NSString stringWithFormat:@"联系电话:%@",[_dic objectForKey:@"phone"]];
    if ([[_dic objectForKey:@"img"] isKindOfClass:[NSData class]]) {
        UIImage  *img = [UIImage imageWithData:[_dic objectForKey:@"img"]];
        _img.image = img;
    }
}
#pragma mark 发送邀请
- (IBAction)yaoqingAction:(UIButton *)sender {
    [self showMessageView:[NSArray arrayWithObjects:[_dic objectForKey:@"phone"], nil] title:@"新信息" body:[NSString stringWithFormat:@"我是来至一米社交里的[%@]，邀请你加入",[AppDelegate shareDelegate].user.nickname]];

}

#pragma mark -messageComposeDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息传送成功
            
            break;
        case MessageComposeResultFailed:
            //信息传送失败
            
            break;
        case MessageComposeResultCancelled:
            //信息被用户取消传送
            
            break;
        default:
            break;
    }
}

-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
//        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        _MScontroller = [[MFMessageComposeViewController alloc] init];
        _MScontroller.recipients = phones;
//        _MScontroller.navigationBar.tintColor = [UIColor redColor];
        _MScontroller.body = body;
        _MScontroller.view.backgroundColor = [UIColor whiteColor];
        _MScontroller.messageComposeDelegate = self;
        [self presentViewController:_MScontroller animated:YES completion:nil];
        [[[[_MScontroller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
        
        UINavigationItem *navigationItem = [[[_MScontroller viewControllers] lastObject] navigationItem];
//        controller.recipients = phones;
//        controller.navigationBar.tintColor = [UIColor redColor];
//        controller.body = body;
//        controller.messageComposeDelegate = self;
//        [self presentViewController:controller animated:YES completion:nil];
//        [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
//
//        UINavigationItem *navigationItem = [[[controller viewControllers] lastObject] navigationItem];
        //xhw2018-1-18改bug15215
        UIButton* ButSign = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];

        [ButSign setTitle:@"取消" forState:UIControlStateNormal];

        ButSign.titleLabel.font = [UIFont systemFontOfSize:18.0];

        [ButSign setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

        [ButSign addTarget:self action:@selector(msgBackFun) forControlEvents:UIControlEventTouchUpInside];

        navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:ButSign];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                        message:@"该设备不支持短信功能"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
        [alert show];
    }
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)msgBackFun

{
    [_MScontroller resignFirstResponder];
    [_MScontroller dismissViewControllerAnimated:YES completion:nil];
    
    
}





@end
